import WechatJSSDK from 'wechat-jssdk/dist/client.umd'

const defaultConfig = {
  appId: 'xxx',
  nonceStr: 'xxx',
  signature: 'xxx',
  timestamp: 'xxx',
  // 下面为可选参数
  // 开启 debug 模式
  debug: true,
  // 设置所有想要使用的微信jsapi列表, 默认值为 ['onMenuShareTimeline', 'onMenuShareAppMessage']，分享到朋友圈及聊天记录
  jsApiList: ['updateAppMessageShareData', 'onMenuShareTimeline', 'onMenuShareAppMessage'],
  customUrl: 'http://res.wx.qq.com/open/js/jweixin-1.6.0.js' // 自定义微信js链接
}

export default (data, config) => {
  console.group('Wechat JSSDK')
  console.log('shareing')
  console.log('shareing data', data)

  const wechatConfig = Object.assign({}, defaultConfig, config)
  console.log(wechatConfig)
  const wechatObj = new WechatJSSDK(wechatConfig)
  // console.log(wechatObj)
  wechatObj
    .initialize()
    .then(wx => {
      console.log('[wx]', wx)
      console.log('[wx.wx]', wx.wx)

      wx.callWechatApi('updateAppMessageShareData', {
        type: 'link',
        title: 'title',
        link: location.href,
        imgUrl: '/logo.png',
        desc: 'description',
        success: function () {},
        cancel: function () {}
      })
      // wx.shareOnChat({
      //   type: 'link',
      //   title: 'title',
      //   link: location.href,
      //   imgUrl: '/logo.png',
      //   desc: 'description',
      //   success: function () {
      //     console.log('success')
      //   },
      //   cancel: function () {
      //     console.log('cancel')
      //   }
      // })

      wx.wx.ready(() => {
        console.log('ready')
      })
      // set up your share info, "w" is the same instance as "wechatObj"
    })
    .catch(err => {
      console.error('[err]', err)
    })

  // 自定义分享到聊天窗口
  // 内部调用 `wechatObj.callWechatApi('onMenuShareAppMessage', {...})`， 语法糖而已
  // wechatObj.shareOnChat({
  //   type: 'link',
  //   title: 'title',
  //   link: location.href,
  //   imgUrl: '/logo.png',
  //   desc: 'description',
  //   success: function () {
  //     console.log('success')
  //   },
  //   cancel: function () {
  //     console.log('cancel')
  //   }
  // })
  // wechatObj.callWechatApi('onMenuShareAppMessage', {
  //   type: 'link',
  //   title: 'title',
  //   link: location.href,
  //   imgUrl: '/logo.png',
  //   desc: 'description',
  //   success: function () {},
  //   cancel: function () {}
  // })
  // // 自定义分享到朋友圈
  // // 语法糖
  // wechatObj.shareOnMoment({
  //   type: 'link',
  //   title: 'title',
  //   link: location.href,
  //   imgUrl: '/logo.png'
  // })
  console.groupEnd('Wechat JSSDK')
}
